package org.xm.sk.controller.dbase;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.xm.sk.exception.DataNotFoundException;
import org.xm.sk.service.data.BaseService;
import org.xm.sk.vo.ob.Ids;
import org.xm.sk.vo.ob.MapList;
import org.xm.sk.vo.ob.PageConf;
import org.xm.sk.vo.ob.ResultMsg;
import org.xm.sk.vo.tb.Role;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@SuppressWarnings("unchecked")
@Api(tags = { "role" })
@RestController
@RequestMapping(value="/roles")
public class RoleController {
    
    @Autowired
    private MessageSource messages;
    @Autowired
    private BaseService<Role> roleService;
    
    @ApiOperation(value="创建角色", notes="根据role对象创建角色，使用此接口需要ADMIN权限")
    @ApiImplicitParam(name = "role", value = "角色详细实体role", required = true, dataType = "Role")
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping(value="", method=RequestMethod.POST)
    public ResultMsg add(@RequestBody Role role) {
        roleService.add(role);
        return new ResultMsg("200", messages.getMessage("success", null, "SUCCESS", null), new Date());
    }

    @ApiIgnore()
    @ApiOperation(value="删除角色", notes="根据URI的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true, paramType = "path", dataType = "int")
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public ResultMsg delete(@PathVariable Integer id) {
        roleService.delete(id);
        return new ResultMsg("200", messages.getMessage("success", null, "SUCCESS", null), new Date());
    }
    
    @ApiOperation(value="删除角色", notes="根据POST的id列表来指定删除对象，可批量")
    @ApiImplicitParam(name = "ids", value = "角色ID集", required = true, dataType = "Ids")
    @RequestMapping(value="/delete", method=RequestMethod.DELETE)
    public ResultMsg Batchdelete(@RequestBody Ids ids) {
        roleService.delete(ids.getIds());
        return new ResultMsg("200", messages.getMessage("success", null, "SUCCESS", null), new Date());
    }
    
    @ApiOperation(value="更新角色详细信息", notes="根据URI的id来指定更新对象，并根据传过来的role信息来更新角色详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, paramType = "path", dataType = "int"),
            @ApiImplicitParam(name = "role", value = "待更新的角色详细实体role", required = true, dataType = "Role")
    })
    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    public ResultMsg update(@PathVariable Integer id, @RequestBody Role role) throws DataNotFoundException {
        Role u = (Role) roleService.findById(id);
        if (null == u) {
            throw new DataNotFoundException(Role.class.getName());
        }
        role.setId(id);
        roleService.update(role);
        return new ResultMsg("200", messages.getMessage("success", null, "SUCCESS", null), new Date());
    }

    @ApiOperation(value="获取角色详细信息", notes="根据URI的id来获取角色详细信息")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true, paramType = "path", dataType = "int")
    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public Role load(@PathVariable Integer id) throws DataNotFoundException {
        Role u = (Role) roleService.findById(id);
        if (null == u) {
            throw new DataNotFoundException(Role.class.getName());
        }
        return u;
    }
    
    @ApiOperation(value="获取角色列表", notes="根据提交的分页信息列出所以角色信息")
    @ApiImplicitParam(name = "pageConf", value = "分页信息实体", required = true, dataType = "PageConf")
    @RequestMapping(value={"/list"}, method=RequestMethod.POST)
    public MapList<Role> list(@RequestBody PageConf pageConf) {
        List<Role> u = null;
        Long allCount = null;
        Object o = roleService.findAllByPage(pageConf);
        if (o instanceof Page) {
            allCount = ((Page<Role>) o).getTotalElements();
            u = ((Page<Role>) o).getContent();
        } else {
            allCount = (long) ((List<Role>) o).size();
            u = (List<Role>) o;
        }
        return new MapList<Role>(allCount, u);
    }

    // --------------------- 业务具体接口 ---------------------
}
